package common

import (
	"errors"
	"gorm.io/gorm"
	"seven-admin/app/admin/schemas/req"
	"seven-admin/app/collection/system"
	"seven-admin/core/config"
	"seven-admin/core/response"
	"seven-admin/util"
)

type IndexService interface {
	Console(auth *req.AuthReq) (res map[string]interface{}, e error)
	Config(domain string) (res map[string]interface{}, e error)
	TenantConfig(auth *req.AuthReq) (res map[string]interface{}, e error)
}

// NewIndexService 初始化
func NewIndexService(db *gorm.DB) IndexService {
	return &indexService{db: db}
}

// indexService 主页服务实现类
type indexService struct {
	db *gorm.DB
}

func (i indexService) Console(auth *req.AuthReq) (res map[string]interface{}, e error) {
	//TODO implement me
	return nil, errors.New("implement me")
}

func (i indexService) Config(domain string) (res map[string]interface{}, e error) {
	//TODO implement me
	var tenant system.AuthTenant
	_ = i.db.Where("domain = ?", domain).First(&tenant).Error
	if tenant.ID == 0 {
		tenant.ID = 1
	}
	sql := i.db
	website, err := util.ConfigUtil.Get(sql, "website", tenant.ID)
	if e = response.CheckErr(err, "Config Get err"); e != nil {
		return
	}
	copyrightStr, err := util.ConfigUtil.GetVal(sql, "website", "copyright", "", tenant.ID)
	if e = response.CheckErr(err, "Config GetVal err"); e != nil {
		return
	}
	var copyright []map[string]string
	if copyrightStr != "" {
		err = util.ToolsUtil.JsonToObj(copyrightStr, &copyright)
		if e = response.CheckErr(err, "Config JsonToObj err"); e != nil {
			return
		}
	} else {
		copyright = []map[string]string{}
	}
	return map[string]interface{}{
		"webName":     website["name"],
		"webLogo":     util.UrlUtil.ToAbsoluteUrl(website["logo"]),
		"webFavicon":  util.UrlUtil.ToAbsoluteUrl(website["favicon"]),
		"webBackdrop": util.UrlUtil.ToAbsoluteUrl(website["backdrop"]),
		"ossDomain":   config.Env.PublicUrl,
		"copyright":   copyright,
		"dept":        config.AdminConfig.SuperTenantId,
	}, nil
}

func (i indexService) TenantConfig(auth *req.AuthReq) (res map[string]interface{}, e error) {
	sql := i.db
	website, err := util.ConfigUtil.Get(sql, "website", auth.TenantId)
	if e = response.CheckErr(err, "Config Get err"); e != nil {
		return
	}
	copyrightStr, err := util.ConfigUtil.GetVal(sql, "website", "copyright", "", auth.TenantId)
	if e = response.CheckErr(err, "Config GetVal err"); e != nil {
		return
	}
	var copyright []map[string]string
	if copyrightStr != "" {
		err = util.ToolsUtil.JsonToObj(copyrightStr, &copyright)
		if e = response.CheckErr(err, "Config JsonToObj err"); e != nil {
			return
		}
	} else {
		copyright = []map[string]string{}
	}
	return map[string]interface{}{
		"webName":     website["name"],
		"webLogo":     util.UrlUtil.ToAbsoluteUrl(website["logo"]),
		"webFavicon":  util.UrlUtil.ToAbsoluteUrl(website["favicon"]),
		"webBackdrop": util.UrlUtil.ToAbsoluteUrl(website["backdrop"]),
		"ossDomain":   config.Env.PublicUrl,
		"copyright":   copyright,
		"dept":        config.AdminConfig.SuperTenantId,
	}, nil
}